<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>Arbitrage.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="Arbitrage code in Java">
<meta NAME="TITLE" CONTENT="Arbitrage code in Java">
<meta NAME="KEYWORDS" CONTENT="Arbitrage,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>Arbitrage.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "Arbitrage.java">Arbitrage.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac Arbitrage.java</span>
<span class="comment"> *  Execution:    java Arbitrage &lt; input.txt</span>
<span class="comment"> *  Dependencies: EdgeWeightedDigraph.java DirectedEdge.java</span>
<span class="comment"> *                BellmanFordSP.java</span>
<span class="comment"> *  Data file:    </span><span class="url">http://algs4.cs.princeton.edu/44sp/rates.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Arbitrage detection.</span>
<span class="comment"> *</span>
<span class="comment"> *  % more rates.txt</span>
<span class="comment"> *  5</span>
<span class="comment"> *  USD 1      0.741  0.657  1.061  1.005</span>
<span class="comment"> *  EUR 1.349  1      0.888  1.433  1.366</span>
<span class="comment"> *  GBP 1.521  1.126  1      1.614  1.538</span>
<span class="comment"> *  CHF 0.942  0.698  0.619  1      0.953</span>
<span class="comment"> *  CAD 0.995  0.732  0.650  1.049  1</span>
<span class="comment"> *</span>
<span class="comment"> *  % java Arbitrage &lt; rates.txt</span>
<span class="comment"> *  1000.00000 USD =  741.00000 EUR</span>
<span class="comment"> *   741.00000 EUR = 1012.20600 CAD</span>
<span class="comment"> *  1012.20600 CAD = 1007.14497 USD</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Arbitrage</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides a client that finds an arbitrage</span>
<span class="comment"> *  opportunity in a currency exchange table by constructing a</span>
<span class="comment"> *  complete-digraph representation of the exchange table and then finding</span>
<span class="comment"> *  a negative cycle in the digraph.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses the Bellman-Ford algorithm to find a</span>
<span class="comment"> *  negative cycle in the complete digraph.</span>
<span class="comment"> *  The running time is proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;&lt;sup&gt;</span><span class="comment">3</span><span class="keyword">&lt;/sup&gt;</span><span class="comment"> in the</span>
<span class="comment"> *  worst case, where </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of currencies.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/44sp"</span><span class="keyword">&gt;</span><span class="comment">Section 4.4</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Arbitrage</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">// this class cannot be instantiated</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">Arbitrage</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  Reads the currency exchange table from standard input and</span>
<span class="comment">     *  prints an arbitrage opportunity to standard output (if one exists).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// V currencies</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> V </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readInt</span><span class="symbol">();</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> name </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> String</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>

<span class="normal">        </span><span class="comment">// create complete network</span>
<span class="normal">        </span><span class="usertype">EdgeWeightedDigraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">EdgeWeightedDigraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            name</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readString</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> w </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> w</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">double</span><span class="normal"> rate </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readDouble</span><span class="symbol">();</span>
<span class="normal">                </span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DirectedEdge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">-</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">log</span><span class="symbol">(</span><span class="normal">rate</span><span class="symbol">));</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// find negative cycle</span>
<span class="normal">        </span><span class="usertype">BellmanFordSP</span><span class="normal"> spt </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BellmanFordSP</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">spt</span><span class="symbol">.</span><span class="function">hasNegativeCycle</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> stake </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1000.0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DirectedEdge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> spt</span><span class="symbol">.</span><span class="function">negativeCycle</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%10.5f %s "</span><span class="symbol">,</span><span class="normal"> stake</span><span class="symbol">,</span><span class="normal"> name</span><span class="symbol">[</span><span class="normal">e</span><span class="symbol">.</span><span class="function">from</span><span class="symbol">()]);</span>
<span class="normal">                stake </span><span class="symbol">*=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">exp</span><span class="symbol">(-</span><span class="normal">e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">());</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"= %10.5f %s</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> stake</span><span class="symbol">,</span><span class="normal"> name</span><span class="symbol">[</span><span class="normal">e</span><span class="symbol">.</span><span class="function">to</span><span class="symbol">()]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"No arbitrage opportunity"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
